package org.zhouhe.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class DataGrid extends TagSupport {
	private static final long serialVersionUID = 1L;
	
	private String url;
	private String title;
	private String height;
	String toolbarId;
	String gridId;
	
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			SimplePage pageTag = (SimplePage) this.getParent();
			gridId = pageTag.getId() +"_grid";
			toolbarId = pageTag.getId() +"_buttons";
			//写入Table开始标签：
			String width = pageTag.getWidth();
			if(width != null){
				width = "width:"+width+(width.indexOf("%")>-1 ? "":"px") +";";
			}else{
				width = "";
			}
			if(height != null){
				height = "height:"+ height +"px;";
			}else{
				height = "";
			}
			out.println("<div data-options=\"region:'center',border:false\">");
			out.println("<table id='"+ gridId +"' class='easyui-datagrid' data-options=\"fit:true,pageSize:20\" ");
			if(title != null){
				out.println(" title='"+ title +"' ");
			}
			if(url != null && url.length()>0){
				out.println(" url='"+ url +"' ");
			}
			out.println(" toolbar='#"+ toolbarId +"' ");
			out.println(" rownumbers='true'  pagination='true'>");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			//写入Table结束标签：
			out.println("</table>");
			out.println("</div>");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}
}
